package main;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import net.TCPServerStarter;
import net.UDPListener;
import utils.Config;

public class Main {
    
    private static final Logger logger = LogManager.getLogger(Main.class);

    public static void main(String[] args) {
        String path = "ConfigFile.ini";
        //String configFile = "config.ini";
        ExecutorService threadPool = Executors.newCachedThreadPool();
        try(InputStream in = new FileInputStream(path);) {
            
            List<Config> configList = Config.loadConfig(in);
            for (Config config : configList) {
                if(config.getPortType().equalsIgnoreCase("UDP")){
                    threadPool.execute(new UDPListener(config));
                }else{
                    threadPool.execute(new TCPServerStarter(config));
                }
                logger.info(config.getName()+ " start "+config.getPortType()+" port "+config.getPort());
            }
            threadPool.shutdown();
        } catch (FileNotFoundException e) {
            logger.error(e);
        } catch (IOException e1) {
            logger.error(e1);
        }

    }

}
